package org.ssssssss.magicapi.script;

import org.ssssssss.script.MagicScript;
import org.ssssssss.script.MagicScriptContext;
import org.ssssssss.script.MagicScriptDebugContext;

import java.util.Map;

/**
 * 脚本管理
 *
 * @author mxd
 */
public class ScriptManager {

	/**
	 * 执行脚本
	 */
	public static Object executeScript(String script, MagicScriptContext context) {
		script = (context instanceof MagicScriptDebugContext ? MagicScript.DEBUG_MARK : "") + script;
		MagicScript magicScript = MagicScript.create(script, null);
		// 执行脚本
		return magicScript.execute(context);
	}

	/**
	 * 执行脚本
	 */
	public static Object executeExpression(String script, MagicScriptContext context) {
		return executeScript("/* generated by execute expression */ return " + script, context);
	}

	/**
	 * 执行脚本
	 */
	public static Object executeExpression(String script, Map<String, Object> paramMap) {
		return executeExpression(script, new MagicScriptContext(paramMap));
	}
}
